package com.lc.rongzecaifu.ui;

import android.view.View;

import com.lc.rongzecaifu.activity.BaseActivity;

/**
 * Created by Administrator on 2017/3/20.
 */
public class WithdrawActivity extends BaseActivity implements View.OnClickListener {
/*

    @BoundView(R.id.withdraw_title_right_tv) private TextView withdraw_title_right_tv;
    @BoundView(R.id.withdraw_bank_ll) private LinearLayout withdraw_bank_ll;
    @BoundView(R.id.withdraw_bank_logo_iv) private SimpleDraweeView withdraw_bank_logo_iv;
    @BoundView(R.id.withdraw_bank_name_tv) private TextView withdraw_bank_name_tv;
    @BoundView(R.id.withdraw_bank_num_tv) private TextView withdraw_bank_num_tv;
    @BoundView(R.id.withdraw_bank_add_ll) private LinearLayout withdraw_bank_add_ll;
    @BoundView(R.id.withdraw_balance_tv) private TextView withdraw_balance_tv;
    @BoundView(R.id.withdraw_money_et) private EditText withdraw_money_et;
    @BoundView(R.id.withdraw_password_et) private EditText withdraw_password_et;
    @BoundView(R.id.withdraw_bt) private Button withdraw_bt;

    private String bank_num;
    private String money;
    private PopupWindow popWindow;

    //银行列表接口  上来显示列表第一个，列表这页不用
    private void getBankListAccessToken() {

        new JsonAccessToken("user/banklist", new AsyCallBack<JsonAccessToken.Info>() {

            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {

                new JsonBanklist(info.access_token, MyApplication.myPreferences.readUid(), new AsyCallBack<JsonBanklist.Info>() {
                    @Override
                    public void onSuccess(String toast, int type, JsonBanklist.Info info) throws Exception {

                        if (info != null) {

                            withdraw_bank_ll.setVisibility(View.VISIBLE);
                            withdraw_bank_name_tv.setText(info.list.get(0).bank_name);
                            bank_num = info.list.get(0).id;
                            withdraw_bank_logo_iv.setImageURI(Uri.parse(info.list.get(0).bank_ico));

                            String string = info.list.get(0).bank_num;
                            String str2 = string.substring(string.length() - 4, string.length());
                            withdraw_bank_num_tv.setText("（" + " 尾号 " + str2 + " " + "）");

                            Log.d("Recharge_bank_name", info.list.get(0).bank_name + "");


                        } else {

                            UtilToast.show(WithdrawActivity.this, JsonBanklist.TOAST);
                        }

                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {

                        withdraw_bank_ll.setVisibility(View.GONE);

                    }

                }).execute(WithdrawActivity.this, true);

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);

                UtilToast.show(WithdrawActivity.this, "安全验证失败！");
            }

        }).execute(this, true);

    }


    //验证用户是否可以提现
    private void getWithdrawvalidateAccessToken() {

        new JsonAccessToken("user/withdraw_validate", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {

                new JsonWithdrawvalidate(info.access_token, MyApplication.myPreferences.readUid(), withdraw_money_et.getText().toString(), withdraw_password_et.getText().toString(), bank_num, new AsyCallBack<JsonWithdrawvalidate.Info>() {

                    @Override
                    public void onSuccess(String toast, int type, JsonWithdrawvalidate.Info info) throws Exception {

                        showPopupWindow(withdraw_bt, JsonWithdrawvalidate.TOAST, true);

                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {

                        showPopupWindow(withdraw_bt, JsonWithdrawvalidate.TOAST, false);

                    }

                }).execute(WithdrawActivity.this, true);

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                UtilToast.show(WithdrawActivity.this, "安全验证失败！");
            }
        }).execute(this, true);

    }

    //提现的接口
    private void getWithdrawAccessToken() {

        new JsonAccessToken("user/withdraw", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {

                new JsonWithdraw(info.access_token, MyApplication.myPreferences.readUid(), withdraw_money_et.getText().toString(), withdraw_password_et.getText().toString(), bank_num, new AsyCallBack<JsonWithdraw.Info>() {

                    @Override
                    public void onSuccess(String toast, int type, JsonWithdraw.Info info) throws Exception {

                        withdraw_balance_tv.setText(info.account_money + "元");
                        MyApplication.myPreferences.saveAccount_money(info.account_money);

                        showPopupWindow(withdraw_bt, JsonWithdraw.TOAST, false);

                        finish();

                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {

                        showPopupWindow(withdraw_bt, JsonWithdraw.TOAST, false);

                    }

                }).execute(WithdrawActivity.this, true);
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                UtilToast.show(WithdrawActivity.this, "安全验证失败！");
            }
        }).execute(this, true);

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_withdraw);

        withdraw_title_right_tv.setOnClickListener(this);
        withdraw_bank_ll.setOnClickListener(this);
        withdraw_bank_add_ll.setOnClickListener(this);
        withdraw_bt.setOnClickListener(this);

        withdraw_balance_tv.setText(MyApplication.myPreferences.readAccount_money() + "元");

        getBankListAccessToken();


    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.withdraw_title_right_tv:

                startActivity(new Intent(this, WithdrawOrRechargeRecordActivity.class).putExtra("mode", true));//true为提现，false为充值

                break;

            case R.id.withdraw_bank_ll:

                startActivityForResult(new Intent(this, ChoseBankActivity.class), 1);

                break;

            case R.id.withdraw_bank_add_ll:

                if (MyApplication.myPreferences.readId_status().equals("0")) {

                    ToastUtils.show(this, "请先通过实名认证，实名认证通过后方可绑定银行卡。");

                } else if (MyApplication.myPreferences.readId_status().equals("2")) {

                    ToastUtils.show(this, "实名认证审核中，实名认证通过后方可绑定银行卡。");

                } else if (MyApplication.myPreferences.readId_status().equals("1")) {

                    startActivity(new Intent(this, BankcardAddActivity.class));

                }

                break;

            case R.id.withdraw_bt:

                money = withdraw_money_et.getText().toString().replaceAll(" ", "");

                if (TextUtils.isEmpty(bank_num)) {
                    ToastUtils.show(this, "请选择提现银行！");
                } else if (TextUtils.isEmpty(money)) {
                    ToastUtils.show(this, "请输入提现金额！");
                } else if (money.equals(".")) {
                    ToastUtils.show(this, "请输入提现金额！");
                } else if ((money.contains(".") && Double.parseDouble(money) < 100) || (!money.contains(".") && Long.parseLong(money) < 100)) {
                    ToastUtils.show(this, "请输入大于100元的金额！");
                } else if (TextUtils.isEmpty(withdraw_password_et.getText().toString())) {
                    ToastUtils.show(this, "请输入支付密码！");
                } else {
                    getWithdrawvalidateAccessToken();
                }

                break;

        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == RESULT_OK && requestCode == 1) {

            withdraw_bank_name_tv.setText(data.getStringExtra("name"));

            withdraw_bank_logo_iv.setImageURI(Uri.parse(data.getStringExtra("ico")));

            String string = data.getStringExtra("num");

            String str2 = string.substring(string.length() - 4, string.length());

            withdraw_bank_num_tv.setText("（" + " 尾号 " + str2 + " " + "）");

            bank_num = data.getStringExtra("id");
        }
    }

    private void showPopupWindow(View parent, String message, final boolean isCan) {

        Log.e("----", message);

        View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) LayoutInflater.from(this)
                .inflate(R.layout.dialog_02, null)));
        //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
        popWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);

        TextView tv01 = (TextView) view.findViewById(R.id.dl_tv_01);//拍照
        TextView tv02 = (TextView) view.findViewById(R.id.dl_tv_02);//相册
        TextView tvmsg = (TextView) view.findViewById(R.id.tv_msg);
        tvmsg.setText(message);
        tv01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                popWindow.dismiss();
            }
        });
        tv02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                if (isCan) {

                    //提现
                    getWithdrawAccessToken();

                }
                popWindow.dismiss();

            }
        });

        //设置动画效果
        popWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        //获取popwindow焦点
        popWindow.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow.setOutsideTouchable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }
*/


}
